﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.Text;

public partial class admin_Home : System.Web.UI.Page
{
    public static int adminID;


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!checkLoginAdmin())
            {
                ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script> alert('You have to login as Admin to access this page!');</script>");
                Response.Redirect("Login.aspx?prePage=adminPage");
            }
            LoadData();

            //Sort
            if (GridView1.Rows.Count > 0)
            {
                GridView1.UseAccessibleHeader = true;
                GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
                //GridView1.FooterRow.TableSection = TableRowSection.TableFooter;
            }
            else
                ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script> alert('Have not other admin!');</script>");
        }
    }
    // check login
    public bool checkLoginAdmin()
    {
        // check session
        if (Session["username"] != null)
        {
            return true;
        }
        else
        {
            // check cookie 
            if (Request.Cookies["username"] != null)
            {
                Session["username"] = Request.Cookies["username"].Value;
                Session["fullname"] = Request.Cookies["fullname"].Value;
                return true;
            }

        }
        return false;
    }
    private void LoadData()
    {
        DataTable dt = AdminManagement.GetAllAdminNotUser(Session["username"].ToString()).Tables["Admins"];
        GridView1.DataSource = dt;
        GridView1.DataBind();

        MultiView1.ActiveViewIndex = 0;
    }


    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        adminID = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());

        if (AdminManagement.DeleteAdmin(adminID))
            LoadData();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        LoadData();
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        adminID = int.Parse(GridView1.DataKeys[e.NewSelectedIndex].Value.ToString());
        MultiView1.ActiveViewIndex = 1;
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }
    protected void btnNewAdmin_Click(object sender, EventArgs e)
    {
        Clear();
        MultiView1.ActiveViewIndex = 1;
    }

    private void Clear()
    {
        TextBox1.Text = " ";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        TextBox6.Text = "";
        txtReEmail.Text = "";
        txtRePass.Text = "";

        TextBox1.Focus();
    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        if (!AdminManagement.CheckUserName(TextBox1.Text))
            regNewAdmin();
        else
            ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script>alert('Username is exist!');</script>");
    }

    private void regNewAdmin()
    {
        string pass = TextBox2.Text;
        byte[] input = Encoding.ASCII.GetBytes(pass);
        //SHA512Managed sha512 = new SHA512Managed();
        //byte[] output = sha512.ComputeHash(input);
        //string passEncoding = Encoding.ASCII.GetString(output);

        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
        byte[] output = md5.ComputeHash(input);
        string passEncoding = Encoding.ASCII.GetString(output);


        if (AdminManagement.AddAdmin(TextBox1.Text, passEncoding, TextBox4.Text, TextBox3.Text, TextBox6.Text))
        {
            LoadData();
            MultiView1.ActiveViewIndex = 0;
        }
        else
            ClientScript.RegisterStartupScript(typeof(Page), "error", "<script>alert('Register Error!');</script>");
    }
    protected void btnCheckUser_Click(object sender, EventArgs e)
    {
        lblCheckUserName.Text = "";
        if (AdminManagement.CheckUserName(TextBox1.Text))
            lblCheckUserName.Text = "UserName is exist!";
        else
            lblCheckUserName.Text = "";
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        Clear();
    }
}